package com.ekgc.addressbook.base.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Component;

@Component
public class PinyinUtils {

    /**
     * 获取汉字字符串的全拼，非汉字保持原样
     * 示例："张三" -> "zhangsan"，"Hello张三" -> "Hellozhangsan"
     */
    public static String getFullPinyin(String chinese) {
        if (chinese == null || chinese.isEmpty()) {
            return "";
        }

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 小写
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不带声调

        StringBuilder result = new StringBuilder();
        for (char c : chinese.toCharArray()) {
            // 判断是否为汉字（Unicode范围：0x4E00-0x9FA5）
            if (isChineseCharacter(c)) {
                try {
                    // 获取字符的拼音数组（可能有多个，取第一个）
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        result.append(pinyinArray[0]);
                    } else {
                        // 特殊汉字可能无法转换，保留原字符
                        result.append(c);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    // 异常处理：记录日志并保留原字符
                    System.err.println("拼音转换异常：" + c + " - " + e.getMessage());
                    result.append(c);
                }
            } else {
                // 非汉字直接拼接
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 获取汉字字符串的声母，非汉字保持原样
     * 示例："张三" -> "ZS"，"Hello张三" -> "HelloZS"
     */
    public static String getInitials(String chinese) {
        if (chinese == null || chinese.isEmpty()) {
            return "";
        }

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE); // 大写
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不带声调

        StringBuilder result = new StringBuilder();
        for (char c : chinese.toCharArray()) {
            if (isChineseCharacter(c)) {
                try {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        // 取拼音的首字母
                        result.append(pinyinArray[0].charAt(0));
                    } else {
                        result.append(c);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    System.err.println("拼音转换异常：" + c + " - " + e.getMessage());
                    result.append(c);
                }
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 判断字符是否为汉字
     */
    private static boolean isChineseCharacter(char c) {
        // 基本汉字范围：0x4E00-0x9FA5
        return c >= 0x4E00 && c <= 0x9FA5;
    }
}